# coding:utf-8

try:
    fh = open("/home/boyun/a.txt", "r")
    fh.write("this is test demo")
except IOError:
    print("Error 没有找到文件或者读取失败")
else:
    print("内容写入成功")
    fh.close()

try:
    fh = open("/home/boyun/b.txt", "r")
finally:
    print(fh.closed)


def raise_exception(level):
    if level < 1:
        raise Exception("Invalid level!")


try:
    raise_exception(0)
except Exception as err:
    print(1, err)
else:
    print(2)


class NetworkError(RuntimeError):
    def __init__(self, arg):
        self.args = arg


try:
    raise NetworkError("Bad hostname")
except NetworkError as e:
    print(e.args)
